// 获取应用实例
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    listHeight: Number,
    menuObj: {},
    flagId: Number,
    menuName: '',
    subMenu: [],
    listArr: [],
    isMore: false, // 是否到底过
    count: '', // 总条数
    offset: 0 // 第一页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 登录
    wx.login({
      success: res => {
        const url = 'https://www.bisaiquan.cn/api/wechat/login'
        const data = {
          code: res.code
        }
        wx.request({
          url: url,
          data: data,
          method: 'POST',
          success(response) {
            wx.setStorageSync('openToken', response.data.data.open_token)
          }
        })
      }
    })

    const _this = this
    let query = wx.createSelectorQuery()
    query.select('#head').boundingClientRect()
    query.exec(function (resel) {
      wx.getSystemInfo({
        success: (res) => {
          _this.setData({
            listHeight: res.windowHeight - resel[0].height
          })
        },
      })
    })

    const id = parseInt(options.menu_id)
    this.setData({
      flagId: id,
      menuName: options.menu_name
    })
  },
  // 子模块
  getSubMenu: function (id, name) {
    const _this = this
    const url = 'content/submenus'
    const data = {
      menu_id: id
    }
    app.$http.post(url, data).then(res => {
      _this.setData({
        menuObj: res.data
      })
      if (!name || name !== 'undefined') {
        wx.setNavigationBarTitle({
          title: name,
        })
      } else {
        wx.setNavigationBarTitle({
          title: _this.data.menuObj.sub_menus[0].menu_name,
        })
      }
    })
  },

  // menu_type ilist 列表，入参  menu_id offset=0
  getThreadsList: function () {
    const url = 'content/threads'
    const data = {
      menu_id: this.data.flagId,
      offset: this.data.offset
    }
    app.$http.post(url, data).then(res => {
      if (res.data.result) {
        const newArr = this.data.listArr.concat(res.data.result)
        this.setData({
          listArr: newArr,
          count: res.data.count,
          offset: res.data.offset
        })
      } else {
        this.setData({
          listArr: []
        })
      }
      wx.stopPullDownRefresh()
    })
  },

  // 列表tab切换
  switchBtnTab: function (ev) {
    const id = parseInt(ev.currentTarget.dataset.menu_id)
    const name = ev.currentTarget.dataset.menu_name
    const type = ev.currentTarget.dataset.menu_type
    const linkUrl = ev.currentTarget.dataset.out_link

    wx.setNavigationBarTitle({
      title: name,
    })
    if (type === 'ipage') {
      const url = `/pages/ipage/ipage?menu_id=${id}`
      app.pageNavigateTo(url)
    } else if (type === 'events') {
      const url = `/pages/events/events?menu_id=${id}&menu_name=${name}`
      wx.redirectTo({
        url: url,
      })
    } else if (type === 'link') {
      const outlink = '/pages/outlink/outlink'
      wx.setStorage({
        key: 'eventUrl',
        data: linkUrl
      })
      app.pageNavigateTo(outlink)
    } else {
      if (id !== this.data.flagId) {
        this.setData({
          isMore: false,
          count: '',
          offset: 0,
          listArr: []
        })
        this.setData({
          flagId: id
        })
        this.getThreadsList()
      }
    }
  },
  // 路由-详情页面
  pageArticleTo: function (ev) {
    const type = ev.currentTarget.dataset.nav_type
    const to = ev.currentTarget.dataset.wthrea_id
    const url = '/pages/article/article'
    const outlink = '/pages/outlink/outlink'
    if (type === 'thread') {
      app.pageNavigateTo(`${url}?wthread_id=${to}`)
    } else {
      wx.setStorage({
        key: 'eventUrl',
        data: to
      })
      app.pageNavigateTo(outlink)
    }


    // var id = event.currentTarget.dataset.wthrea_id
    // var url = `/pages/article/article?wthread_id=${id}`
    // app.pageNavigateTo(url)

  },
  // 回到首页
  backIndexTo: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getSubMenu(this.data.flagId, this.data.menuName)
    this.getThreadsList()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      isMore: false,
      count: '',
      offset: 0,
      listArr: []
    })
    this.getThreadsList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const count = parseInt(this.data.count)
    const offset = this.data.offset
    if (!this.data.isMore) {
      if (offset === count) {
        wx.showToast({
          title: '到底了，没有更多数据了',
          icon: 'none',
          duration: 2000
        })
        this.setData({
          isMore: true
        })
      } else {
        this.getThreadsList()
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})